//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.SCorderInQueryController');
	com.yonyou.ncmm_pac_pda.SCorderInQueryController = function() {
		com.yonyou.ncmm_pac_pda.SCorderInQueryController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$SCorderInQueryController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//    
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$SCorderInQueryController$evaljs(js) {
		eval(js)
	}

	function com$yonyou$ncmm_pac_pda$SCorderInQueryController$cancle(sender, args) {
		$view.close();
	}
	// 打开参照页面
	function openRefPage(ncbillcode, ctxnamekey,ctxpkkey,pkfiel) {
		var jsonparam = {};
		getRefParam(ncbillcode, function(data) {
			jsonparam = data;
		});

		if(jsonparam == null) {
			//$alert("jsonparam is null!");
			return;
		}

		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.RefPage", //目标页面
			isKeep: "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			data: jsonparam,
			callback: function() {
				var refdata = $param.getJSONObject("data");
				$ctx.put(ctxnamekey, refdata["name"]);
				$ctx.put(ctxpkkey, refdata[pkfiel]);
				$ctx.dataBind();
				$cache.write("default" + ctxnamekey, $jsonToString(refdata));

				//设置了默认库存组织后，自动下载默认库存组织下的仓库
				//where pk_org='库存组织编码'
				if(ctxnamekey == "stockorg") {
					var condition = {
						"pk_org": "='" + refdata["code"] + "'"
					};
					processDocDataFromNC(StordocVO, condition);
				}
			}
		});
	}

	//获取参照vo的vo信息
	//reftype： 外系统调用标识
	function getRefParam(reftype, callback) {
		var condition = {
			"ncbillcode": reftype
		};
		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var jsondata = {
				bdlistdata: eval(data)
			};
			var reftitle = (jsondata.bdlistdata)[0]["appbillname"];
			if((jsondata.bdlistdata)[0]["downloadnum"] == "" || (jsondata.bdlistdata)[0]["downloadnum"] == "0") {
				$alert("【" + reftitle + "】没有可参照的数据");
				if(callback != null && callback != undefined && typeof(callback) == 'function') {
					callback(null);
				}
				return;
			}
			var reftablename = (jsondata.bdlistdata)[0]["apptablename"]; //本地表名
			var jsonparam = {
				reftype: reftype, //外系统调用标识
				reftitle: reftitle, //元数据显示名称
				reftablename: reftablename //本地表名
			};
			if(callback != null && callback != undefined && typeof(callback) == 'function') {
				callback(jsonparam);
			}
		});
	}
	//物料参照
	function com$yonyou$ncmm_pac_pda$SCorderInQueryController$materialRef(sender, args) {
		openRefPage("PDAMATERIAL", "qmaterial","cmaterialvid","pk_material");

	}
	//部门参照
	function com$yonyou$ncmm_pac_pda$SCorderInQueryController$deptRef(sender, args) {
		openRefPage("PDADEPT", "qdept","cdeptid","pk_dept");

	}	
	//供应商
	function com$yonyou$ncmm_pac_pda$SCorderInQueryController$supplierRef(sender, args){
       openRefPage("PDASUPPLIER", "qsupplier","pk_supplier","pk_supplier");
    }
	//查询
	function com$yonyou$ncmm_pac_pda$SCorderInQueryController$doQuery(sender, args) {
		var condition = {};
		$ctx.dataCollect();
		//物料 
		if($id("qmaterial").get("value") != "") {
			condition["pk_material"] = " = '" + $ctx.getString("cmaterialvid") + "'";
		}
		//单据号
		if($id("qbillcode").get("value") != "") {
			condition["vbillcode"] = " = '" + $id("qbillcode").get("value").toString() + "'";
		}
		//采购部门
		if($id("qdept").get("value") != "") {
			condition["pk_dept"] = " = '" + $ctx.getString("cdeptid") + "'";
		}
		//采购员
		//if($id("qemployee").get("value")!=""){
		//condition["cemployeeid_name"] = " = '" + $id("qemployee").get("value").toString() + "'";
		//}
		//供应商
		if($id("qsupplier").get("value") != "") {
			condition["pk_supplier"] = " = '" + $ctx.getString("pk_supplier") + "'";
		}
		//批次号
		if($id("qbatchcode").get("value") != "") {
			condition["vbatchcode"] = " = '" + $id("qbatchcode").get("value").toString() + "'";
		}
		if($id("qbilldatestar").get("value") != "" && $id("qbilldateend").get("value") != "") {
			condition["dbilldate"] = "between '" + $id("qbilldatestar").get("value").toString() + " 00:00:00" + "' and '" + $id("qbilldateend").get("value").toString() + " 23:59:59" + "'";
		} else {
			//单据日期从		
			if($id("qbilldatestar").get("value") != "") {
				condition["dbilldate"] = " >= '" + $id("qbilldatestar").get("value").toString() + " 00:00:00'";
			}
			//单据日期到
			if($id("qbilldateend").get("value") != "") {
				condition["dbilldate"] = " <= '" + $id("qbilldateend").get("value").toString() + " 23:59:59'";
			}
		}
		$view.closeWithCallBack({
			"qcondition": condition
		});
	}

	function com$yonyou$ncmm_pac_pda$SCorderInQueryController$onload(sender, args) {
		var json = {};
		var nowdate = NCMMPubUtil.getNowDate();
		json["qbilldatestar"] = nowdate;
		json["qbilldateend"] = nowdate;
		//	$alert("json="+$jsonToString(json));
		$ctx.push(json);
	}
//清空
function com$yonyou$ncmm_pac_pda$SCorderInQueryController$doClear(sender, args){
      var json={};
      $ctx.push(json);
}
	com.yonyou.ncmm_pac_pda.SCorderInQueryController.prototype = {
    supplierRef : com$yonyou$ncmm_pac_pda$SCorderInQueryController$supplierRef,
    doClear : com$yonyou$ncmm_pac_pda$SCorderInQueryController$doClear,
		onload: com$yonyou$ncmm_pac_pda$SCorderInQueryController$onload,
		doQuery: com$yonyou$ncmm_pac_pda$SCorderInQueryController$doQuery,
		deptRef: com$yonyou$ncmm_pac_pda$SCorderInQueryController$deptRef,
		materialRef: com$yonyou$ncmm_pac_pda$SCorderInQueryController$materialRef,
		cancle: com$yonyou$ncmm_pac_pda$SCorderInQueryController$cancle,
		initialize: com$yonyou$ncmm_pac_pda$SCorderInQueryController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$SCorderInQueryController$evaljs
	};
	com.yonyou.ncmm_pac_pda.SCorderInQueryController.registerClass('com.yonyou.ncmm_pac_pda.SCorderInQueryController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}